/*
 * Copyright 2021 TiKV Project Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.webank.tikv;

import com.webank.tikv.kvproto.Kvrpcpb;
import com.webank.tikv.util.UriUtils;

import java.io.Serializable;
import java.net.URI;
import java.util.List;
import java.util.Objects;

public class TiConfiguration implements Serializable {

    private List<URI> pdAddrs = UriUtils.addrsToUrls(new String[]{"127.0.0.1:2379"});

    private long timeout = 200;
    private long warmUpTimeout = 300;
    private long pdFirstGetMemberTimeout = 5000;
    private int maxFrameSize = 512 * 1024 * 1024; // 512MB
    private long connRecycleTime = 60 * 1000;

    private Kvrpcpb.CommandPri commandPriority = Kvrpcpb.CommandPri.Normal;
    private Kvrpcpb.IsolationLevel isolationLevel = Kvrpcpb.IsolationLevel.SI;

    private String zone;
    private String resource = ""; // 自身来源标识

    private int grpcHealthCheckTimeout = 100; // ms
    private int healthStoreCheckPeriod = 3 * 1000;
    private int healthPdCheckPeriod = 10 * 1000;
    private int refreshRegionPeriod = 5 * 60 * 1000;

    private boolean tlsEnable = false;
    private long certReloadInterval = 10 * 1000;

    private String trustCertCollectionFile;
    private String keyCertChainFile;
    private String keyFile;

    private boolean useJks = false;
    private String jksKeyPath;
    private String jksKeyPassword;
    private String jksTrustPath;
    private String jksTrustPassword;

    private boolean warmUpEnable = true;

    private int keepAliveTime = 10 * 1000;
    private int keepAliveTimeout = 3 * 1000;
    private int idleTimeout = 60 * 1000;

    private int scanRegionsLimit = 1000;

    public TiConfiguration() {
    }

    public static TiConfiguration createDefault(List<String> pdAddrs) {
        Objects.requireNonNull(pdAddrs, "pdAddrs is null");
        TiConfiguration conf = new TiConfiguration();
        conf.pdAddrs = UriUtils.addrsToUrls(pdAddrs);
        return conf;
    }

    public long getTimeout() {
        return timeout;
    }

    public TiConfiguration setTimeout(long timeout) {
        this.timeout = timeout;
        return this;
    }

    public long getWarmUpTimeout() {
        return warmUpTimeout;
    }

    public TiConfiguration setWarmUpTimeout(long timeout) {
        this.warmUpTimeout = timeout;
        return this;
    }

    public long getPdFirstGetMemberTimeout() {
        return pdFirstGetMemberTimeout;
    }

    public TiConfiguration setPdFirstGetMemberTimeout(long pdFirstGetMemberTimeout) {
        this.pdFirstGetMemberTimeout = pdFirstGetMemberTimeout;
        return this;
    }

    public List<URI> getPdAddrs() {
        return pdAddrs;
    }

    public int getMaxFrameSize() {
        return maxFrameSize;
    }

    public TiConfiguration setMaxFrameSize(int maxFrameSize) {
        this.maxFrameSize = maxFrameSize;
        return this;
    }

    public long getConnRecycleTime() {
        return connRecycleTime;
    }

    public TiConfiguration setConnRecycleTime(int connRecycleTime) {
        this.connRecycleTime = connRecycleTime;
        return this;
    }

    public Kvrpcpb.CommandPri getCommandPriority() {
        return commandPriority;
    }

    public TiConfiguration setCommandPriority(Kvrpcpb.CommandPri commandPriority) {
        this.commandPriority = commandPriority;
        return this;
    }

    public Kvrpcpb.IsolationLevel getIsolationLevel() {
        return isolationLevel;
    }

    public TiConfiguration setIsolationLevel(Kvrpcpb.IsolationLevel isolationLevel) {
        this.isolationLevel = isolationLevel;
        return this;
    }

    public String getZone() {
        return zone;
    }

    public TiConfiguration setZone(String zone) {
        this.zone = zone;
        return this;
    }

    public String getResource() {
        return resource;
    }

    public TiConfiguration setResource(String resource) {
        this.resource = resource;
        return this;
    }

    public long getGrpcHealthCheckTimeout() {
        return this.grpcHealthCheckTimeout;
    }

    public TiConfiguration setGrpcHealthCheckTimeout(int grpcHealthCheckTimeout) {
        this.grpcHealthCheckTimeout = grpcHealthCheckTimeout;
        return this;
    }

    public int getHealthStoreCheckPeriod() {
        return healthStoreCheckPeriod;
    }

    public void setHealthStoreCheckPeriod(int healthStoreCheckPeriod) {
        this.healthStoreCheckPeriod = healthStoreCheckPeriod;
    }

    public int getHealthPdCheckPeriod() {
        return healthPdCheckPeriod;
    }

    public void setHealthPdCheckPeriod(int healthPdCheckPeriod) {
        this.healthPdCheckPeriod = healthPdCheckPeriod;
    }

    public int getRefreshRegionPeriod() {
        return refreshRegionPeriod;
    }

    public void setRefreshRegionPeriod(int refreshRegionPeriod) {
        this.refreshRegionPeriod = refreshRegionPeriod;
    }

    public int getKeepAliveTime() {
        return keepAliveTime;
    }

    public TiConfiguration setKeepAliveTime(int keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
        return this;
    }

    public int getKeepAliveTimeout() {
        return keepAliveTimeout;
    }

    public TiConfiguration setKeepAliveTimeout(int timeout) {
        this.keepAliveTimeout = timeout;
        return this;
    }

    public int getIdleTimeout() {
        return idleTimeout;
    }

    public TiConfiguration setIdleTimeout(int timeout) {
        this.idleTimeout = timeout;
        return this;
    }

    public boolean isWarmUpEnable() {
        return warmUpEnable;
    }

    public TiConfiguration setWarmUpEnable(boolean warmUpEnable) {
        this.warmUpEnable = warmUpEnable;
        return this;
    }

    public boolean isTlsEnable() {
        return tlsEnable;
    }

    public long getCertReloadInterval() {
        return certReloadInterval;
    }

    public TiConfiguration setCertReloadInterval(long interval) {
        this.certReloadInterval = interval;
        return this;
    }

    public TiConfiguration setTlsEnable(boolean tlsEnable) {
        this.tlsEnable = tlsEnable;
        return this;
    }

    public String getTrustCertCollectionFile() {
        return trustCertCollectionFile;
    }

    public TiConfiguration setTrustCertCollectionFile(String trustCertCollectionFile) {
        this.trustCertCollectionFile = trustCertCollectionFile;
        return this;
    }

    public String getKeyCertChainFile() {
        return keyCertChainFile;
    }

    public TiConfiguration setKeyCertChainFile(String keyCertChainFile) {
        this.keyCertChainFile = keyCertChainFile;
        return this;
    }

    public String getKeyFile() {
        return keyFile;
    }

    public TiConfiguration setKeyFile(String keyFile) {
        this.keyFile = keyFile;
        return this;
    }

    public boolean isJksEnable() {
        return useJks;
    }

    public TiConfiguration setJksEnable(boolean useJks) {
        this.useJks = useJks;
        return this;
    }

    public String getJksKeyPath() {
        return jksKeyPath;
    }

    public TiConfiguration setJksKeyPath(String jksKeyPath) {
        this.jksKeyPath = jksKeyPath;
        return this;
    }

    public String getJksKeyPassword() {
        return jksKeyPassword;
    }

    public TiConfiguration setJksKeyPassword(String jksKeyPassword) {
        this.jksKeyPassword = jksKeyPassword;
        return this;
    }

    public String getJksTrustPath() {
        return jksTrustPath;
    }

    public TiConfiguration setJksTrustPath(String jksTrustPath) {
        this.jksTrustPath = jksTrustPath;
        return this;
    }

    public String getJksTrustPassword() {
        return jksTrustPassword;
    }

    public TiConfiguration setJksTrustPassword(String jksTrustPassword) {
        this.jksTrustPassword = jksTrustPassword;
        return this;
    }

    public int getScanRegionsLimit() {
        return scanRegionsLimit;
    }

    public TiConfiguration setScanRegionsLimit(int scanRegionsLimit) {
        this.scanRegionsLimit = scanRegionsLimit;
        return this;
    }

}
